import 'package:dss_base/net/base_entity.dart';
import 'package:dss_base/net/http_util.dart';
import 'package:dss_base/widgets/base/state_holder.dart';
import 'package:flutter/cupertino.dart';

class BaseViewModel extends ChangeNotifier {
  ValueNotifier<LoadingDialogState> dialogState = ValueNotifier(LoadingDialogState.init);

  /// 加载对话框 - 显示
  void showLoadingDialog() {
    dialogState.value = LoadingDialogState.show;
  }

  /// 加载对话框 - 隐藏
  void hideLoadingDialog() {
    dialogState.value = LoadingDialogState.hide;
  }

  void setStateChanged(){
    notifyListeners();
  }

  /// 省事的加载逻辑
  void request<T>({
    bool isShowLoadingDialog = true,
    required Future<BaseEntity> request,
    required NetConvert<T>? onConvert,
    required NetSuccessCallbackT<T> onSuccess,
    NetFailureCallback? onFailure,
  }) {
    if (isShowLoadingDialog) showLoadingDialog();
    HttpUtil.instance
        .request<T>(
          request: request,
          onConvert: onConvert,
          onSuccess: onSuccess,
          onFailure: onFailure,
        )
        .whenComplete(() {
          if (isShowLoadingDialog) hideLoadingDialog();
        });
  }
}
